package com.wlstock.funi.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.wlstock.funi.model.OperateModel;

public class OperateDao {
	
	private Dao<OperateModel, Integer> operateDao ;
	private DataBaseHelper helper;
	private static OperateDao daoInstance;
	
	@SuppressWarnings("unchecked")
	private OperateDao(Context context){
		try {
			helper = DataBaseHelper.getHelper(context);
			operateDao = helper.getDao(OperateModel.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public static OperateDao getInstance(Context mContext){
		if (daoInstance == null) {
			synchronized (OperateDao.class) {
				if (daoInstance == null) {
					daoInstance = new OperateDao(mContext);
				}
			}
		}
		return daoInstance;
	}
	
	public void addOperation(OperateModel model){
		try {
			operateDao.createOrUpdate(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public OperateModel queryModelByStockHoldDetailId(String StockHoldDetailId) {
		OperateModel model = null;
		try {
			model = operateDao.queryBuilder().where().eq("StockHoldDetailId", StockHoldDetailId).queryForFirst();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return model;
	}
	
	public OperateModel queryModelByStockId(String StockHoldID){
		
		OperateModel model = null;
		try {
			model = operateDao.queryBuilder().where().eq("StockHoldID", StockHoldID).queryForFirst();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return model;
	}
	
	public OperateModel queryModelByFundIdDescByTradeDate(String fundid){
		OperateModel model = null;
		try {
			model = operateDao.queryBuilder().orderBy("TradedDate", false).where().eq("fundid", fundid).queryForFirst();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return model;
	}
	
//	/**
//	 * @param fundId 操盘手ID
//	 * @param season 当前月份的ID 如2015-11 则传入的是201511
//	 * @return
//	 */
//	public int getFundNameRange(int fundId,long season){
//		int i = 12;
//		try {
//			List<OperateModel> list = this.getOperList(fundId,season);
//			if (list!=null && list.size()>0) {
//				for (int j = 0; j < list.size()	; j++) {
//					if (list.get(j).getFundid()==fundId) {
//						i = j;
//						break;
//					}                              
//				}
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//		}
//		return i;
//	}

	/**
	 * 
	 * @param fundId 操盘手ID
	 * @param season 当前月份的ID 如2015-11 则传入的是201511
	 * @return
	 */
	public List<OperateModel> getOperList(long season)  {
		List<OperateModel> list = new ArrayList<OperateModel>();
		try {
			list = operateDao.queryBuilder().orderBy("ProfitRate", false).groupBy("fundid").where().eq("season", season).query();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	
	public List<OperateModel> getOperateModels(){
		List<OperateModel> list = new ArrayList<OperateModel>();
		try {
			list = operateDao.queryBuilder().orderBy("ProfitRate", false).query();
		} catch (SQLException e) {
			// TODO: handle exception
		}
		return list;
	}
	
	public List<OperateModel> queryAll(){
		List<OperateModel> list = new ArrayList<OperateModel>();
		try {
			list = operateDao.queryBuilder().orderBy("TradedDate", false).query();
		} catch (SQLException e) {
			// TODO: handle exception
		}
		return list;
	}
	
}
